﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.Data;

namespace HitachiApp.Model
{
    public class ScheduleUnitWeekControlModel : ScheduleUnitBaseModel, INotifyPropertyChanged
    {
        DateTime dt;
        List<ScheduleUnitDayControlModel> unit_days = new List<ScheduleUnitDayControlModel>();
        SchedulePolicy policy;
        int action_type;
        ScheduleTabViewModel parent;

        public ScheduleUnitWeekControlModel(DateTime datetime, int type, ScheduleTabViewModel parent_model)
            : base(type)
        {
            parent = parent_model;
            action_type = type;
            dt = datetime;
            unit_days.Add(new ScheduleUnitDayControlModel(dt.AddDays(0), type, parent_model));
            unit_days.Add(new ScheduleUnitDayControlModel(dt.AddDays(1), type, parent_model));
            unit_days.Add(new ScheduleUnitDayControlModel(dt.AddDays(2), type, parent_model));
            unit_days.Add(new ScheduleUnitDayControlModel(dt.AddDays(3), type, parent_model));
            unit_days.Add(new ScheduleUnitDayControlModel(dt.AddDays(4), type, parent_model));
            unit_days.Add(new ScheduleUnitDayControlModel(dt.AddDays(5), type, parent_model));
            unit_days.Add(new ScheduleUnitDayControlModel(dt.AddDays(6), type, parent_model));
        }
        public bool IsEmptyDay(int day)
        {
            int c = 0;
            c += unit_days[day].Morning.UnitTimeProjects.Count;
            c += unit_days[day].Afternoon.UnitTimeProjects.Count;
            c += unit_days[day].Evening.UnitTimeProjects.Count;
            return c == 0;
        }
        public SchedulePolicy Policy
        {
            get { return policy; }
            set { policy = value; }
        }
        public DateTime StartTime
        {
            get { return dt; }
        }
        public ScheduleUnitDayControlModel Mon
        {
            get { return unit_days[0]; }
        }
        public ScheduleUnitDayControlModel Tue
        {
            get { return unit_days[1]; }
        }
        public ScheduleUnitDayControlModel Wed
        {
            get { return unit_days[2]; }
        }
        public ScheduleUnitDayControlModel Thi
        {
            get { return unit_days[3]; }
        }
        public ScheduleUnitDayControlModel Fri
        {
            get { return unit_days[4]; }
        }
        public ScheduleUnitDayControlModel Sat
        {
            get { return unit_days[5]; }
        }
        public ScheduleUnitDayControlModel Sun
        {
            get { return unit_days[6]; }
        }

        public bool MonEnable
        {
            get {
                if (policy == null) return false;
                return (action_type == Schedule.WholeYear) ^ policy.CommonMask[0];
            }
            set
            {
                bool b = policy.CommonMask[0] = value ^ (action_type == Schedule.WholeYear);
                parent.UpdatePolicy(0, b);
            }
        }
        public bool TueEnable
        {
            get {
                if (policy == null) return false;
                return (action_type == Schedule.WholeYear) ^ policy.CommonMask[1];
            }
            set
            {
                bool b = policy.CommonMask[1] = value ^ (action_type == Schedule.WholeYear);
                parent.UpdatePolicy(1, b);
            }
        }
        public bool WedEnable
        {
            get
            {
                if (policy == null) return false;
                return (action_type == Schedule.WholeYear) ^ policy.CommonMask[2];
            }
            set
            {
                bool b = policy.CommonMask[2] = value ^ (action_type == Schedule.WholeYear);
                parent.UpdatePolicy(2, b);
            }
        }
        public bool ThiEnable
        {
            get
            {
                if (policy == null) return false;
                return (action_type == Schedule.WholeYear) ^ policy.CommonMask[3];
            }
            set
            {
                bool b = policy.CommonMask[3] = value ^ (action_type == Schedule.WholeYear);
                parent.UpdatePolicy(3, b);
            }
        }
        public bool FriEnable
        {
            get
            {
                if (policy == null) return false;
                return (action_type == Schedule.WholeYear) ^ policy.CommonMask[4];
            }
            set
            {
                bool b = policy.CommonMask[4] = value ^ (action_type == Schedule.WholeYear);
                parent.UpdatePolicy(4, b);
            }
        }
        public bool SatEnable
        {
            get
            {
                if (policy == null) return false;
                return (action_type == Schedule.WholeYear) ^ policy.CommonMask[5];
            }
            set
            {
                bool b = policy.CommonMask[5] = value ^ (action_type == Schedule.WholeYear);
                parent.UpdatePolicy(5, b);
            }
        }
        public bool SunEnable
        {
            get
            {
                if (policy == null) return false;
                return (action_type == Schedule.WholeYear) ^ policy.CommonMask[6];
            }
            set
            {
                bool b = policy.CommonMask[6] = value ^ (action_type == Schedule.WholeYear);
                parent.UpdatePolicy(6, b);
            }
        }

        public void UpdatePolicy()
        {
            RaisePropertyChanged("MonEnable");
            RaisePropertyChanged("TueEnable");
            RaisePropertyChanged("WedEnable");
            RaisePropertyChanged("ThiEnable");
            RaisePropertyChanged("FriEnable");
            RaisePropertyChanged("SatEnable");
            RaisePropertyChanged("SunEnable");
        }

        public void UpdatePolicy(int d)
        {
            switch (d)
            {
                case 0:
                    RaisePropertyChanged("MonEnable");
                    break;
                case 1:
                    RaisePropertyChanged("TueEnable");
                    break;
                case 2:
                    RaisePropertyChanged("WedEnable");
                    break;
                case 3:
                    RaisePropertyChanged("ThiEnable");
                    break;
                case 4:
                    RaisePropertyChanged("FriEnable");
                    break;
                case 5:
                    RaisePropertyChanged("SatEnable");
                    break;
                case 6:
                    RaisePropertyChanged("SunEnable");
                    break;
                default:
                    break;
            }
        }

        public void Clear()
        {
            unit_days[0].Clear();
            unit_days[1].Clear();
            unit_days[2].Clear();
            unit_days[3].Clear();
            unit_days[4].Clear();
            unit_days[5].Clear();
            unit_days[6].Clear();
        }

        public void Cleanup()
        {
            unit_days[0].Cleanup();
            unit_days[1].Cleanup();
            unit_days[2].Cleanup();
            unit_days[3].Cleanup();
            unit_days[4].Cleanup();
            unit_days[5].Cleanup();
            unit_days[6].Cleanup();
        }

        public void Add(ViewItem vi)
        {
            Project p = vi.Project;
            Schedule s = GetSchedule(p);
            DayOfWeek dow = s.PlanDate.DayOfWeek;
            switch (dow)
            {
                case DayOfWeek.Monday:
                    Mon.Add(vi);
                    break;
                case DayOfWeek.Tuesday:
                    Tue.Add(vi);
                    break;
                case DayOfWeek.Wednesday:
                    Wed.Add(vi);
                    break;
                case DayOfWeek.Thursday:
                    Thi.Add(vi);
                    break;
                case DayOfWeek.Friday:
                    Fri.Add(vi);
                    break;
                case DayOfWeek.Saturday:
                    Sat.Add(vi);
                    break;
                case DayOfWeek.Sunday:
                    Sun.Add(vi);
                    break;
                default:
                    break;
            }
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
